#include <UIKit/UIKit.h>

@interface BBOCHookTestObj

- (void)test;

@end

%hook BBOCHookTestObj

- (void)test
{    
    %log;
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"exchen" delegate:self
        cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];

    return %orig();
}

%end

@interface BBAppDelegate
- (void)applicationWillResignActive:(UIApplication *)application;
@end

%hook BBAppDelegate
- (void)applicationWillResignActive:(UIApplication *)application {
    puts("hook applicationWillResignActive");
    return %orig();
}
%end

FILE *fopen(const char *path, const char *mode);

// The hook is thus made
%hookf(FILE *, fopen, const char *path, const char *mode) {
	puts("Hey, we're hooking fopen to deny relative paths!");
	printf("path=%s", path);
	return %orig; // Call the original implementation of this function
}

void exit(int);
%hookf(void, exit) {
	puts("hook exit");
}


// static void (*original_cydiaCheck)(void);  // a function pointer to store the original.
// void replaced_cydiaCheck(void) {
//     printf("[+] Enter a return value for cydiaCheck");
// }

// %ctor {
//     //If Exported || If we have symbols
//             //MSImageRef image = MSGetImageByName("/var/root/a.out");
//             //Look for our target method
//             //void *sym = MSFindSymbol(image, "_getAge");
//     //Else
//     unsigned int *sym = (unsigned int *)0xdeadbeef;

//     //Hook the symbol we found
//     MSHookFunction((void *)sym, (void *)replaced_cydiaCheck, (void **)&original_cydiaCheck);
// }

// void cydiaCheck();
// void (*oldCydiaCheck)();
// void newCydiaCheck() {
//     printf("hook cydia");
// }



void (*old_myCMethod)();
void new_myCMethod() {
    printf("hook myCMethod\n");
    // %orig();
    old_myCMethod();
}

%ctor{
    // MSHookFunction(&cydiaCheck, &newCydiaCheck, &oldCydiaCheck);
    // MSHookFunction(&myCMethod, &new_myCMethod, &old_myCMethod);
    const char *imgPath = [[NSBundle mainBundle] executablePath].UTF8String;
    MSImageRef image = MSGetImageByName(imgPath);
    void (*myCMethod)();
    myCMethod = (void(*)())MSFindSymbol(image, "_myCMethod");
    if (myCMethod) {
        MSHookFunction((void *)myCMethod, (void *)new_myCMethod, (void **)&old_myCMethod);
    }
    printf("Tweak ctor\n");
}
